home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Dots & Pixels / sources / movingnoisedots.cp < prev    next >
Text File  |  1995-09-29  |  3KB  |  118 lines

  1. #include <Retrace.h>
  2. #include <Devices.h>
  3. #include <SegLoad.h>
  4. #include <Timer.h>
  5.  
  6. #include <math.h>
  7. #include <iostream.h>
  8.  
  9. #include "C_randomizer.h"
  10. #include "flowsettings.h"
  11. #include "vretrace.h"
  12. // #include "macutilities.h"
  13. #include "phaser.h"
  14. #include "screenarea.h"
  15. #include "screendots.h"
  16. #include "dotcollection.h"
  17. #include "movingnoisedots.h"
  18.  
  19. movingnoisedots::movingnoisedots( int numbits,
  20.     int xpos, int ypos, int aantaldots, int lifetime, int noiseBits)
  21.     : dotcollection( aantaldots)
  22.     , phaser( lifetime)
  23.     , screendots( numbits, xpos, ypos, aantaldots)
  24.     , noiseMask( 0x00010001L * ((1 << noiseBits) - 1))
  25.     , noiseShift( (1 << noiseBits) >> 1)
  26. {}
  27.  
  28. movingnoisedots::movingnoisedots( int numbits,
  29.     screen_position where, int aantaldots, int lifetime, int noiseBits)
  30.     : dotcollection( aantaldots)
  31.     , phaser( lifetime)
  32.     , screendots( numbits, where, aantaldots)
  33.     , noiseMask( 0x00010001L * ((1 << noiseBits) - 1))
  34.     , noiseShift( (1 << noiseBits) >> 1)
  35. {}
  36.  
  37. void movingnoisedots::compute_addresses()
  38. {
  39.     move_the_dots();
  40.     //
  41.     // could use dotcollection::numdots, as well
  42.     //
  43.     #define help_the_optimizer
  44.     
  45.     #ifdef help_the_optimizer
  46.         const int loopend = screendots::numdots;
  47.         const int the_shift = coord_shift;    // aids the optimizer
  48.         unsigned char **dot_address = dot_addresses;
  49.         short *xcoord = xcoords;
  50.         short *ycoord = ycoords;
  51.         for( int i = 0; i < loopend; i++)
  52.         {
  53.             *dot_address++ =
  54.                 &screen[ *ycoord++ >> the_shift][ *xcoord++ >> the_shift];
  55.         }
  56.     #else
  57.         for( int i = 0; i < screendots::numdots; i++)
  58.         {
  59.             dot_addresses[ i] =
  60.                 &screen[ ycoords[ i] >> coord_shift][ xcoords[ i] >> coord_shift];
  61.         }
  62.     #endif
  63.     #undef help_the_optimizer
  64. }
  65.  
  66. void movingnoisedots::move_the_dots()
  67. {
  68.     (void)major_step();
  69.     
  70.     short *the_xcoord = xcoords;
  71.     short *the_ycoord = ycoords;
  72.     //
  73.     // Note: there is _exactly_ one '*the_xcoord++ = …' and _exactly_ one
  74.     // '*the_ycoord++ = …' in every possible path through the loop.
  75.     //
  76.     short_long_hack the_hack;
  77.     const int loopend = dotcollection::numdots; // could also use screendots::numdots
  78.     
  79.     for( int i = 0; i < loopend; i++)    
  80.     {
  81.         if( minor_step() != 0)
  82.         {
  83.             //
  84.             // compute new position of dot
  85.             //
  86.             const long prev_x = (long) *the_xcoord;
  87.             const long prev_y = (long) *the_ycoord;
  88.             //
  89.             // determine a direction to move in:
  90.             // (for now, we simply choose a fixed noise level and use that)
  91.             //
  92.             the_hack.ulong = randomizer_step() & noiseMask;
  93.             const long new_x = ((long) *the_xcoord) + (long)the_hack.shorties.left  - noiseShift;
  94.             const long new_y = ((long) *the_ycoord) + (long)the_hack.shorties.right - noiseShift;
  95.  
  96.             if( (new_x != (short)new_x) || (new_y != (short)new_y))
  97.             {
  98.                 //
  99.                 // use one call of 'step' and split the result in two:
  100.                 //
  101.                 the_hack.ulong = randomizer_step();
  102.                 *the_xcoord++ = the_hack.shorties.left;
  103.                 *the_ycoord++ = the_hack.shorties.right;
  104.             } else {
  105.                 *the_xcoord++ = new_x;
  106.                 *the_ycoord++ = new_y;
  107.             }
  108.         } else {
  109.             //
  110.             // dot surpassed its lifetime
  111.             //
  112.             the_hack.ulong = randomizer_step();
  113.             *the_xcoord++ = the_hack.shorties.left;
  114.             *the_ycoord++ = the_hack.shorties.right;
  115.         }
  116.     }
  117. }
  118.